<?php 
/*
 module:		浏览记录
 create_time:	2022-04-08 19:57:48
 author:	陕西格创
 contact:		
*/

namespace app\api\controller;

use app\api\service\ZhBrowseRecordService;
use app\api\model\ZhBrowseRecord as ZhBrowseRecordModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
use app\api\model\Member as MemberModel;
use app\api\model\SysSite as SysSiteModel;

class ZhBrowseRecord extends Common {


	/**
	* @api {post} /ZhBrowseRecord/add 01、添加
	* @apiGroup ZhBrowseRecord
	* @apiVersion 1.0.0
	* @apiDescription  添加

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"
	* @apiParam (输入参数：) {string}			wxapp_id 平台id 
	* @apiParam (输入参数：) {int}				u_id 用户id 
	* @apiParam (输入参数：) {int}				auth_id 发布id 
	* @apiParam (输入参数：) {string}			createtime 创建时间 

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" 201","msg":"操作失败"}
	*/
	function add(){
        $wxapp_id = $this->request->post('wxapp_id');
        $uId      = $this->request->uid;
        $authId   = $this->request->post('auth_id','','serach_in');// 发布信息id
        if(!$wxapp_id){
            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
        if(!$wxSetting){
            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
        }
        
        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if(!$user){
            return $this->ajaxReturn($this->errorCode,'未知用户');
        }
        if($user['is_vip'] == 1 && $user['expires_time'] > time()){
            return $this->ajaxReturn($this->successCode,'用户是会员');
        }
        if(!$authId){
            return $this->ajaxReturn($this->errorCode,'缺少信息参数');
        }
        $time = date('Y-m-d',time());
        $idsLength = ZhBrowseRecordModel::where([
                                        'wxapp_id'  => $wxapp_id,
                                        'u_id'      => $uId,
                                        'createtime'=> $time
                                    ])->count('id');
        $findData = ZhBrowseRecordModel::where([
                                        'wxapp_id'  => $wxapp_id,
                                        'u_id'      => $uId,
                                        'createtime'=> $time,
                                        'auth_id'   => $authId
                                    ])->find();
        if($idsLength >= $wxSetting['no_vip_frequency'] && !$findData){
            $res = ['is'=>0];
            return $this->ajaxReturn($this->successCode,'操作成功',$res);
        }else{
            if(!$findData){
                $res = ZhBrowseRecordModel::insert([
                    'wxapp_id'  => $wxapp_id,
                    'u_id'      => $uId,
                    'createtime'=> $time,
                    'auth_id'   => $authId
                ]);
                return $this->ajaxReturn($this->successCode,'操作成功',$res);
            }else{
                return $this->ajaxReturn($this->successCode,'操作成功');
            }
        }
	}



}

